/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate.validator;

import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimaps;
import com.google.dspl.model.DataType;
import com.google.dspl.model.Identifier;
import com.google.dspl.model.Value;
import com.google.dspl.validate.ValidationContext;

import java.util.Set;

/**
 * Utilities to validate a reference to another item.
 *
 * @author Shardul Deo
 */
class ValidationHelper {

  public static boolean isValidConceptReference(Identifier identifier, ValidationContext context) {
    if (Strings.isNullOrEmpty(identifier.getDatasetId()) || Strings.isNullOrEmpty(
        identifier.getObjectId())) {
      return false;
    }
    if (context.getDatasetResolver().getConcept(identifier) == null) {
      return false;
    }
    return true;
  }

  public static boolean isValidTopicReference(Identifier identifier, ValidationContext context) {
    if (Strings.isNullOrEmpty(identifier.getDatasetId()) || Strings.isNullOrEmpty(
        identifier.getObjectId())) {
      return false;
    }
    if (context.getDatasetResolver().getTopic(identifier) == null) {
      return false;
    }
    return true;
  }

  public static boolean isValidTableReference(Identifier identifier, ValidationContext context) {
    if (Strings.isNullOrEmpty(identifier.getDatasetId()) || Strings.isNullOrEmpty(
        identifier.getObjectId())) {
      return false;
    }
    if (context.getDatasetResolver().getTable(identifier) == null) {
      return false;
    }
    return true;
  }

  private static final ImmutableMultimap<DataType, DataType> TYPE_RESTRICTION =
      ImmutableMultimap.<DataType, DataType>builder()
      .putAll(DataType.STRING, DataType.STRING)
      .putAll(DataType.FLOAT, DataType.FLOAT, DataType.STRING)
      .putAll(DataType.INTEGER, DataType.INTEGER, DataType.FLOAT, DataType.STRING)
      .putAll(DataType.BOOLEAN, DataType.BOOLEAN, DataType.STRING)
      .putAll(DataType.DATE, DataType.DATE, DataType.STRING)
      .putAll(DataType.CONCEPT, DataType.CONCEPT)
      .build();

  private static final ImmutableMultimap<DataType, DataType> TYPE_ALLOWED =
      ImmutableMultimap.copyOf(
          Multimaps.invertFrom(TYPE_RESTRICTION, ArrayListMultimap.<DataType, DataType>create()));

  /**
   * @return If the subType is the same or more restrictive than the superType.
   */
  public static boolean isMoreRestrictiveType(DataType subType, DataType superType) {
    return TYPE_RESTRICTION.get(subType).contains(superType);
  }

  /**
   * @return the {@link DataType}s that are more restrictive than the given type.
   */
  public static Set<DataType> getMoreRestrictiveTypes(DataType superType) {
    return ImmutableSet.copyOf(TYPE_ALLOWED.get(superType));
  }

  /**
   * Check that the value has a value for the given type and only for the given type.
   */
  public static boolean isValidValueOfType(Value value, DataType dataType) {
    return ((dataType == DataType.STRING) ^ value.getStringValue() == null)
        && ((dataType == DataType.FLOAT) ^ value.getFloatValue() == null)
        && ((dataType == DataType.INTEGER) ^ value.getIntegerValue() == null)
        && ((dataType == DataType.BOOLEAN) ^ value.getBooleanValue() == null)
        && ((dataType == DataType.DATE) ^ value.getDateValue() == null)
        && ((dataType == DataType.CONCEPT) ^ value.getConceptValue() == null);
  }

  private ValidationHelper() {
    throw new AssertionError("Do not instantiate");
  }
}
